<div class="layui-card">
    <div class="layui-card-header">
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>玩家管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <form class="layui-form toolbar nowrap">
            搜索：
            <input id="test-search-value" class="layui-input search-input" type="text" placeholder="输入ID"/>&emsp;
            <button id="test-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </form>
        <table class="layui-table" id="test-playerTable" lay-filter="table-playerTable"></table>
    </div>
</div>

<script type="text/html" id="playerTableDemo">

    <a class="layui-btn layui-btn-xs" lay-event="addBlackList">添加黑名单</a>
    {{#  if(d.online == '1'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="kickPlayer">踢出玩家</a>
    {{#  } }}
</script>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','laydate','layer'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var layer = layui.layer;
        // 渲染表格
        table.render({
            elem: '#test-playerTable',
            url: config.base_server + 'api-operation/playerList/getPlayerAll',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                <!--{type: 'numbers'},-->
                {field: 'id', sort: true,width:170, title: '玩家ID'},
                {field: 'createdOn', sort: true,width:170,templet: function (d) {
                    return util.toDateString(d.createdOn);
                }, title: '创建时间'},
//                {field: 'dayByTotal', sort: true,width:80, title: '总在线天数'},
                {field: 'timeByTotal',align:'center', sort: true,width:150,templet: function (d) {
                    var date = d.timeByTotal / 1000 / 60 / 60 ;
                    date = date.toFixed(2);
                    return date;
                }, title: '总在线时长(小时)'},
                {field: 'guest', sort: true,width:80,templet: function (d) {
                    return d.guest == 0 ? "非游客":"游客";
                },title: '游客'},
                {field: 'ip', sort: true,width:150, title: '登录IP'},
                {field: 'loginOn', sort: true,width:170,templet: function (d) {
                    return util.toDateString(d.loginOn);
                }, title: '登录时间'},
                {field: 'logoutOn', sort: true,width:170,templet: function (d) {
                    return util.toDateString(d.logoutOn);
                }, title: '登出时间'},
                {field: 'online', sort: true,width:80, templet: function (d) {
                    return d.online == 0 ? "离线":"在线";
                }, title: '在线'},
                {field: 'state', sort: true,width:100, templet: function (d) {
                    return d.state == 0 ? "非冻结":"冻结";
                }, title: '冻结'},
                {fixed: 'right',  align:'left', toolbar: '#playerTableDemo'}
            ]]
        });
        //监听工具条:lay-event对应值
        table.on('tool(table-playerTable)', function (obj) {
            var data = obj.data;
            var event = obj.event;
            if (event === 'addBlackList') {
                addBlackList(data);
            }else if (event === 'kickPlayer'){
                kickPlayer(data);
            }
        });

        var kickPlayer = function (data) {
            if (data.online == 0){
                return alert("该玩家未在线,不用踢出");
            }
            $.ajax({
                type : 'GET',
                url : config.base_server + 'api-operation/playerList/kickPlayer',
                data : {access_token : config.getToken().access_token,id : data.id} ,
                error : function(data) {
                    alert(data.resp_msg);
                },
                success : function(data) {
                    alert(data.resp_msg);
                }
            });
            table.reload('test-playerTable');
        };

        var addBlackList = function (data) {
            $.ajax({
                type : 'GET',
                url : config.base_server + 'api-operation/playerList/addBlackList',
                data : {access_token : config.getToken().access_token,
                    id : data.id} ,
                error : function(data) {
                    alert(data.resp_msg);
                },
                success : function(data) {
                    alert(data.resp_msg);
                }
            });
            table.reload('test-playerTable');
        };

        // 搜索按钮点击事件
        $('#test-btn-search').click(function () {
            var id = $('#test-search-value').val();
            table.reload('test-playerTable', {where: {id : id}});
        });
    });
</script>
